package zsc.ruanc.practicaltraining.utils;


import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.model.result.ResultCode;

/**
 * 对返回结果的包装，无需重复写new对象的语句
 */
public class ResultUtils {

    public static<T> JsonResult success(T data) {
        return new JsonResult(ResultCode.SUCCESS, data, true);
    }

    public static<T> JsonResult success(T data, String info) {
        JsonResult jsonResult = new JsonResult(ResultCode.SUCCESS, data, true);
        jsonResult.setInfo(info);
        return jsonResult;
    }

    public static JsonResult success() {
        return new JsonResult(ResultCode.SUCCESS, true);
    }

    /**
     * 根据错误码找到对应的枚举
     * @param code 错误码
     * @param data 数据
     * @param <T>
     * @return
     */
    public static<T> JsonResult error(int code, T data) {
        ResultCode e = ResultCode.findCode(code);
        return new JsonResult(e, data, false);
    }

    public static JsonResult error(int code) {
        ResultCode e = ResultCode.findCode(code);
        return new JsonResult(e, false);
    }

    /**
     * 返回错误码为-1的JsonResule
     */
    public static JsonResult error() {
        return new JsonResult(ResultCode.ERROR, false);
    }

    public static JsonResult error(int code, String info) {
        ResultCode e = ResultCode.findCode(code);
        JsonResult jsonResult = new JsonResult(e, false);
        jsonResult.setInfo(info);
        return jsonResult;
    }

}
